#include "opengl.hpp"
#include "glFont2.h"

namespace curling {

texture_font_t::texture_font_t()
{
  glGenTextures(1, &_font_texture);
  _glfont = boost::shared_ptr<glfont::GLFont>(new glfont::GLFont());
  assert(_glfont->Create("data/arial10.glf", _font_texture));
}

texture_font_t::~texture_font_t()
{
}

double texture_font_t::string_width( const std::string& text )
{
  std::pair<int, int> size;
  _glfont->GetStringSize(text, &size);
  return size.first;
}

void texture_font_t::render_string( const std::string& text, double scale )
{
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  _glfont->Begin();
  _glfont->DrawString(text, 0.1, 0, 0);
  glDisable(GL_BLEND);
  glDisable(GL_TEXTURE_2D);
}

void Start_Menu_Textures::initialize() {

	textureIDs[SWEDEN_FLAG_ITEM] = 0;
	loadTexture("data/SwedenFlag.tga", textureIDs[SWEDEN_FLAG_ITEM], false);

	textureIDs[CANADA_FLAG_ITEM] = 0;
	loadTexture("data/CanadianFlag.tga", textureIDs[CANADA_FLAG_ITEM], false);

	textureIDs[PLAY_ITEM] = 0;
	loadTexture("data/PlayGIMP.tga", textureIDs[PLAY_ITEM], false); 
	
	textureIDs[PLAY_MARKED_ITEM] = 0;
	loadTexture("data/PlayGIMPMarked.tga", textureIDs[PLAY_MARKED_ITEM], true);

	textureIDs[OPTIONS_ITEM] = 0;
	loadTexture("data/gamesettings.tga", textureIDs[OPTIONS_ITEM], false); 
	
	textureIDs[OPTIONS_MARKED_ITEM] = 0;
	loadTexture("data/gamesettingsMarked.tga", textureIDs[OPTIONS_MARKED_ITEM], true);

	textureIDs[QUIT_ITEM] = 0;
	loadTexture("data/QuitGIMP.tga", textureIDs[QUIT_ITEM], true);

	textureIDs[QUIT_MARKED_ITEM] = 0;
	loadTexture("data/QuitGIMPMarked.tga", textureIDs[QUIT_MARKED_ITEM], true);

	textureIDs[TEAM_ONE_WINS_ITEM] = 0;
	loadTexture("data/TeamOneWins.tga", textureIDs[TEAM_ONE_WINS_ITEM], true);

	textureIDs[TEAM_TWO_WINS_ITEM] = 0;
	loadTexture("data/TeamTwoWins.tga", textureIDs[TEAM_TWO_WINS_ITEM], true);

	textureIDs[TEAM_ONE_TROPHY_ITEM] = 0;
	loadTexture("data/TrophyTeamOne.tga", textureIDs[TEAM_ONE_TROPHY_ITEM], true);

	textureIDs[TEAM_TWO_TROPHY_ITEM] = 0;
	loadTexture("data/TrophyTeamTwo.tga", textureIDs[TEAM_TWO_TROPHY_ITEM], true);

	textureIDs[TIE_ITEM] = 0;
	loadTexture("data/tie.tga", textureIDs[TIE_ITEM], true);

	textureIDs[TIE_TROPHY] = 0;
	loadTexture("data/TrophyTie.tga", textureIDs[TIE_TROPHY], true);
}

void Start_Menu_Textures::loadTexture(const char *filename, unsigned int &_id, bool pureAlpha) {


  GLFWimage image;
  assert(glfwReadImage(filename, &image, GLFW_ORIGIN_UL_BIT) != 0);

  // Make a texture object and bind it.
  glGenTextures(1, &_id);
  glBindTexture(GL_TEXTURE_2D, _id);

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  // For pure alpha textures, grayscale gets interpreted as alpha
  if (pureAlpha && image.Format == GL_LUMINANCE)
	image.Format = GL_ALPHA;

  // Upload the pixel data.
  glTexImage2D(GL_TEXTURE_2D, 0, image.Format, image.Width, image.Height,
			   0, image.Format, GL_UNSIGNED_BYTE, reinterpret_cast<void*>(image.Data));
  glfwFreeImage(&image);
}


void Start_Menu_Textures::deinitialize() {
	 // Release the texture object.
    #ifndef __APPLE_CC__
	for (int i = 0; i < MENU_ITEMS::NR_OF_ITEMS; ++i)
		glDeleteTextures(1, &textureIDs[i]);
	#else
    for (int i = 0; i < NR_OF_ITEMS; ++i)
        glDeleteTextures(1, &textureIDs[i]);
    #endif
}

void Start_Menu_Textures::setColor(float r, float g, float b, float a) {

  glColor4f(r, g, b, a);
}

void Start_Menu_Textures::renderTexture(float x, float y, float width, float height, const unsigned int textureID) {
	glEnable(GL_TEXTURE_2D);
	// Bind the texture of this sprite and draw a quad with it.
		glBindTexture(GL_TEXTURE_2D, textureIDs[textureID]);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

		glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2f(x, y);
			glTexCoord2f(1, 0);
			glVertex2f(x + width, y);
			glTexCoord2f(1, 1);
			glVertex2f(x + width, y + height);
			glTexCoord2f(0, 1);
			glVertex2f(x, y + height);
		glEnd();
		glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);
}

void Start_Menu_Textures::renderTexture(float x, float y, const unsigned int textureID, const bool flipY) {
	glEnable(GL_TEXTURE_2D);

	// Bind the texture of this sprite and draw a quad with it.
		glBindTexture(GL_TEXTURE_2D, textureIDs[textureID]);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		
		glBegin(GL_QUADS);
			glTexCoord2f(0, flipY);
			glVertex2f(0.0, 0.0);

			glTexCoord2f(1, flipY);
			glVertex2f(x, 0.0);

			glTexCoord2f(1, !flipY);
			glVertex2f(x, y);

			glTexCoord2f(0, !flipY);
			glVertex2f(0.0, y);
		glEnd();
	glDisable(GL_TEXTURE_2D);
}

}